home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GSUTIL.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  5KB  |  159 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsutil.c */
  20. /* Utilities for Ghostscript library */
  21. #include "std.h"
  22. #include "string_.h"
  23. #include "memory_.h"
  24. #include "gsprops.h"
  25. #include "gsutil.h"            /* for prototype checking */
  26.  
  27. /* Export the size of a gs_prop_item for clients. */
  28. const uint gs_prop_item_sizeof = sizeof(gs_prop_item);
  29.  
  30. /* ------ String utilities ------ */
  31.  
  32. /* Compare two strings, returning -1 if the first is less, */
  33. /* 0 if they are equal, and 1 if first is greater. */
  34. /* We can't use memcmp, because we always use unsigned characters. */
  35. int
  36. bytes_compare(const byte *s1, uint len1, const byte *s2, uint len2)
  37. {    register uint len = len1;
  38.     if ( len2 < len ) len = len2;
  39.        {    register const byte *p1 = s1;
  40.         register const byte *p2 = s2;
  41.         while ( len-- )
  42.             if ( *p1++ != *p2++ )
  43.                 return (p1[-1] < p2[-1] ? -1 : 1);
  44.        }
  45.     /* Now check for differing lengths */
  46.     return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
  47. }
  48.  
  49. /* Test whether a string matches a pattern with wildcards. */
  50. /* '*' = any substring, '?' = any character, '\' quotes next character. */
  51. private const string_match_params smp_default = { '*', '?', '\\', 0 };
  52. int
  53. string_match(const byte *str, uint len, const byte *pstr, uint plen,
  54.   register const string_match_params *psmp)
  55. {    const byte *pback = 0;
  56.     const byte *p = pstr, *pend = pstr + plen;
  57.     const byte *sp = str, *spend = str + len;
  58.     uint matched = 0;
  59.     if ( psmp == 0 )
  60.         psmp = &smp_default;
  61.     while ( p < pend )
  62.     {    register byte ch = *p;
  63.         if ( ch == psmp->any_substring )
  64.         {    pback = ++p, matched = 0;
  65.             continue;
  66.         }
  67.         else if ( ch == psmp->any_char )
  68.         {    if ( sp == spend ) return 0;    /* str too short */
  69.             p++, sp++, matched++;
  70.             continue;
  71.         }
  72.         else if ( ch == psmp->quote_next )
  73.         {    if ( ++p == pend ) return 1;    /* bad pattern */
  74.             ch = *p;
  75.         }
  76.         if ( sp == spend ) return 0;    /* str too short */
  77.         if ( *sp == ch || psmp->ignore_case && (*sp ^ ch) == 0x20 &&
  78.              (ch &= ~0x20) >= 0x41 && ch <= 0x5a
  79.            )
  80.             p++, sp++, matched++;
  81.         else if ( pback == 0 )
  82.             return 0;    /* no * to back up to */
  83.         else
  84.            {    sp += 1 - matched;
  85.             p = pback;
  86.             matched = 0;
  87.            }
  88.     }
  89.     return 1;
  90. }
  91.  
  92. /* Compute a hash for a string */
  93. uint
  94. string_hash(const byte *ptr, uint len)
  95. {    register const byte *p = ptr;
  96.     register uint hash = 0;
  97.     register uint n = len;
  98.     while ( n-- ) hash = hash * 19 + *p++;
  99.     return hash;
  100. }
  101.  
  102. /* ------ Property list utilities ------ */
  103.  
  104. /* Extract known properties from a list.  See gsprops.h for more details. */
  105. int
  106. props_extract(gs_prop_item *plist, int count,
  107.   const gs_prop_item *template, int tcount,
  108.   gs_prop_item **pknown, int finished)
  109. {    const gs_prop_item *pti;
  110.     gs_prop_item **ppi;
  111.     gs_prop_item *pli;
  112.     int i, j;
  113.     for ( pti = template, ppi = pknown, j = 0; j < tcount; pti++, ppi++, j++ )
  114.        {    const char *tstr = pti->pname;
  115.         int tlen = pti->name_size;
  116.         if ( tstr == 0 ) continue;    /* no name */
  117.         if ( tlen < 0 ) tlen = strlen(tstr);
  118.         *ppi = 0;
  119.         for ( pli = plist, i = 0; i < count; pli++, i++ )
  120.           if ( pli->status == pv_set )
  121.            {    const char *lstr = pli->pname;
  122.             int llen = pli->name_size;
  123.             if ( lstr == 0 ) continue;
  124.             if ( llen < 0 ) llen = strlen(lstr);
  125.             if ( llen == tlen && !memcmp(tstr, lstr, llen) )
  126.                {    /* Names match, check types. */
  127.                 if ( pli->type != pti->type )
  128.                  { /* Check for int [array] -> float [array] */
  129.                    if ( pli->type == prt_int &&
  130.                     pti->type == prt_float
  131.                       )
  132.                      pli->type = prt_float,
  133.                      pli->value.f = pli->value.i;
  134.                    else if ( pli->type == prt_int_array &&
  135.                          pti->type == prt_float_array
  136.                        )
  137.                     { int i;
  138.                       gs_prop_item *vp = pli->value.a.p.v;
  139.                       pli->type = prt_float_array;
  140.                       for ( i = pli->value.a.size; --i >= 0; vp++ )
  141.                     vp->type = prt_float,
  142.                     vp->value.f = vp->value.i;
  143.                     }
  144.                    else
  145.                     { pli->status = pv_typecheck;
  146.                       break;
  147.                     }
  148.                  }
  149.                 *ppi = pli, pli->status = pv_OK;
  150.                 break;
  151.                }
  152.            }
  153.        }
  154.     if ( finished )
  155.       for ( pli = plist, i = 0; i < count; pli++, i++ )
  156.         if ( pli->status != pv_OK ) pli->status = pv_unknown;
  157.     return 0;
  158. }
  159.